package cn.iocoder.yudao.module.social.controller.app.container;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.social.controller.admin.container.vo.ContainerPageReqVO;
import cn.iocoder.yudao.module.social.controller.app.container.vo.AppContainerCreateReqVO;
import cn.iocoder.yudao.module.social.controller.app.container.vo.AppContainerRespVO;
import cn.iocoder.yudao.module.social.controller.app.container.vo.AppContainerUpdateReqVO;
import cn.iocoder.yudao.module.social.convert.container.AppContainerConvert;
import cn.iocoder.yudao.module.social.dal.dataobject.container.ContainerDO;
import cn.iocoder.yudao.module.social.service.container.ContainerService;
import cn.iocoder.yudao.module.social.service.container.ContainerPermissionService;
import cn.iocoder.yudao.module.social.service.container.AppContainerConvertService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 容器")
@RestController
@RequestMapping("/social/container")
@Validated
public class AppContainerController {

    @Resource
    private ContainerService containerService;

    @Resource
    private ContainerPermissionService containerPermissionService;

    @Resource
    private AppContainerConvertService appContainerConvertService;

    @PostMapping("/create")
    @Operation(summary = "创建容器")
    public CommonResult<Long> createContainer(@Valid @RequestBody AppContainerCreateReqVO createReqVO) {
        // 设置当前登录用户为创建者
        Long userId = getLoginUserId();

        // 如果没有设置权限，默认为公开
        if (createReqVO.getPermission() == null) {
            createReqVO.setPermission("public");
        }

        // 转换为管理端VO并设置创建者
        var adminCreateReqVO = AppContainerConvert.INSTANCE.convertToAdmin(createReqVO, userId);
        return success(containerService.createContainer(adminCreateReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新容器")
    public CommonResult<Boolean> updateContainer(@Valid @RequestBody AppContainerUpdateReqVO updateReqVO) {
        // 检查权限
        ContainerDO container = containerService.getContainer(updateReqVO.getId());
        Long userId = getLoginUserId();
        if (!containerPermissionService.canEdit(container, userId)) {
            return CommonResult.error(403, "无权限编辑此容器");
        }

        // 转换为管理端VO
        var adminUpdateReqVO = AppContainerConvert.INSTANCE.convertToAdmin(updateReqVO);
        containerService.updateContainer(adminUpdateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除容器")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteContainer(@RequestParam("id") Long id) {
        // 检查权限
        ContainerDO container = containerService.getContainer(id);
        Long userId = getLoginUserId();
        if (!containerPermissionService.canDelete(container, userId)) {
            return CommonResult.error(403, "无权限删除此容器");
        }

        containerService.deleteContainer(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得容器")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppContainerRespVO> getContainer(@RequestParam("id") Long id) {
        ContainerDO container = containerService.getContainer(id);
        Long userId = getLoginUserId();

        // 检查查看权限
        if (!containerPermissionService.canView(container, userId)) {
            return CommonResult.error(403, "无权限查看此容器");
        }

        return success(appContainerConvertService.convertWithPermissions(container, userId));
    }

    @GetMapping("/page")
    @Operation(summary = "获得容器分页")
    public CommonResult<PageResult<AppContainerRespVO>> getContainerPage(@Valid ContainerPageReqVO pageReqVO) {
        PageResult<ContainerDO> pageResult = containerService.getContainerPage(pageReqVO);
        Long userId = getLoginUserId();

        // 过滤用户可见的容器并添加权限信息
        return success(appContainerConvertService.filterAndConvertPage(pageResult, userId));
    }

    @GetMapping("/list-by-type")
    @Operation(summary = "根据类型获得容器列表")
    @Parameter(name = "type", description = "容器类型", required = true, example = "board")
    public CommonResult<List<AppContainerRespVO>> getContainerListByType(@RequestParam("type") String type) {
        List<ContainerDO> list = containerService.getContainerListByType(type);
        Long userId = getLoginUserId();

        // 过滤用户可见的容器并添加权限信息
        return success(appContainerConvertService.filterAndConvertList(list, userId));
    }

    @GetMapping("/list-by-parent")
    @Operation(summary = "根据父容器获得子容器列表")
    @Parameter(name = "parentId", description = "父容器编号", required = true, example = "1")
    public CommonResult<List<AppContainerRespVO>> getContainerListByParent(@RequestParam("parentId") Long parentId) {
        List<ContainerDO> list = containerService.getContainerListByParent(parentId);
        Long userId = getLoginUserId();

        // 过滤用户可见的容器并添加权限信息
        return success(appContainerConvertService.filterAndConvertList(list, userId));
    }

    @GetMapping("/my-list")
    @Operation(summary = "获得我创建的容器列表")
    public CommonResult<List<AppContainerRespVO>> getMyContainerList() {
        Long userId = getLoginUserId();
        List<ContainerDO> list = containerService.getContainerListByCreator(String.valueOf(userId));
        return success(appContainerConvertService.convertListWithPermissions(list, userId));
    }

    @GetMapping("/tree")
    @Operation(summary = "获得容器树形结构")
    @Parameter(name = "type", description = "容器类型", example = "board")
    public CommonResult<List<AppContainerRespVO>> getContainerTree(@RequestParam(value = "type", required = false) String type) {
        List<ContainerDO> list = containerService.getContainerTree(type);
        Long userId = getLoginUserId();

        // 过滤用户可见的容器并添加权限信息
        return success(appContainerConvertService.filterAndConvertList(list, userId));
    }

    @GetMapping("/public-list")
    @Operation(summary = "获得公开容器列表")
    @Parameter(name = "type", description = "容器类型", example = "board")
    public CommonResult<List<AppContainerRespVO>> getPublicContainerList(@RequestParam(value = "type", required = false) String type) {
        List<ContainerDO> list = containerService.getPublicContainerList(type);
        Long userId = getLoginUserId();
        return success(appContainerConvertService.convertListWithPermissions(list, userId));
    }

}
